package model.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import Hibernate.dao.Classroom;
import Hibernate.dao.ClassroomDAO;
import Hibernate.dao.Expert;

public class ClassroomService {
	Map<String,String> map;
	ClassroomDAO classroomDao;
	
	public void setClassroomDao(ClassroomDAO classroomDao) {
		this.classroomDao = classroomDao;
	}

	public String addClassroom(String classroomInfo) {
		// TODO Auto-generated method stub
		map = new HashMap<String,String>();
		System.out.println(classroomInfo);
		try{
			Classroom tempClassroom = new Classroom();
			JSONObject jclassroomInfo = JSONObject.fromObject(classroomInfo);
			if(jclassroomInfo.containsKey("epclassId")){
				if(jclassroomInfo.containsKey("limit")){
					tempClassroom.setEpclassId(jclassroomInfo.getString("epclassId"));
					tempClassroom.setLimitNo(jclassroomInfo.getInt("limit"));
					if(classroomDao.findById(jclassroomInfo.getString("epclassId")) != null){
						map.put("flag", "false");
						map.put("reason", "该考场已经存在");
					}else{
						classroomDao.save(tempClassroom);
						map.put("flag", "true");
					}
				}else{
					map.put("flag", "false");
					map.put("reason", "考场编号不能为空");
				}
			}else{
				map.put("flag", "false");
				map.put("reason", "考场编号不能为空");
			}
		}catch (JSONException e){
			map.put("flag", "false");
			map.put("reason", "数据格式不正确");
		}
		return JSONObject.fromObject(map).toString();
	}

	public String classroomAll() {
		// TODO Auto-generated method stub
		map = new HashMap<String,String>();
		JSONArray classroomJson = new JSONArray();
		List list = classroomDao.findAll();
		for(int i=0;i<list.size();i++){
			Classroom tempClassroom = (Classroom) list.get(i);
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("epclassId",tempClassroom.getEpclassId());
			jsonObject.put("limitNo",tempClassroom.getLimitNo());
			jsonObject.put("nowNo",tempClassroom.getNowNo());
			jsonObject.put("status",tempClassroom.getStatus());
			classroomJson.add(jsonObject);
		}
		map.put("flag","true");
		map.put("data", classroomJson.toString());
		return JSONObject.fromObject(map).toString();
	}
	
	public String ExcelUpload(File file,String FileName)
	{
		map = new HashMap<String,String>();
		System.out.println(FileName);
			if(FileName==null||!FileName.endsWith(".xls"))
			{
				map.put("flag", "false");
				map.put("Info", "文件名为空 or 文件类型错误");
			}
			else if(file==null||!file.exists())
			{
				map.put("flag", "false");
				map.put("Info", "此文件不存在！");
			}else
			{
				return ExcelToClassroom(file);
			}
			return JSONObject.fromObject(map).toString();
	}
	
	public String ExcelToClassroom(File file) {
		map = new HashMap<String, String>();
		Map<String, String> value = new HashMap<String, String>();
		ArrayList<String> list = new ArrayList<String>();
		try {
			// 创建对Excel工作簿文件的引用
			HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(file));
			// 在Excel文档中，第一张工作表的缺省索引是0
			// 其语句为：HSSFSheet sheet = workbook.getSheetAt(0);
			HSSFSheet sheet = wookbook.getSheet("mAdmins");
			// 获取到Excel文件中的所有行数
			int rows = sheet.getPhysicalNumberOfRows();
			// 遍历行
			for (int i = 0; i < rows; i++) {
				// 读取左上端单元格
				HSSFRow row = sheet.getRow(i);
				// 行不为空
				if (row != null) {
					// 获取到Excel文件中的所有的列
					int cells = row.getPhysicalNumberOfCells();
					// StringBuilder value = new StringBuilder();
					// StringBuilder info = new StringBuilder();
					// 遍历列
					for (int j = 0; j < cells; j++) {
						// 获取到列的值
						HSSFCell cell = row.getCell(j);
						if (cell != null) {
							switch (cell.getCellType()) {
							case HSSFCell.CELL_TYPE_FORMULA:
								break;
							case HSSFCell.CELL_TYPE_NUMERIC:
								if (i == 0)
									list.add(Double.toString(cell
											.getNumericCellValue()));
								else
									value.put(list.get(j), Double.toString(cell
											.getNumericCellValue()));
								break;
							case HSSFCell.CELL_TYPE_STRING:
								if (i == 0)
									list.add(cell.getStringCellValue().trim());
								else
									value.put(list.get(j), cell
											.getStringCellValue().trim());
								break;
							default:
								value.put(list.get(j), "");
								break;
							}
						}
					}
					if (i != 0) {
						for (int n = cells; n < list.size(); n++) {
							value.put(list.get(n), "");
						}
						Classroom classroom = new Classroom();
						classroom.setInfo(value);
						classroom.setNowNo(0);
						classroom.setStatus(0);
						classroomDao.attachDirty(classroom);
					}
				}
			}
			wookbook.close();
			map.put("flag", "true");
			map.put("Info", "success");
		} catch (FileNotFoundException e) {
			map.put("flag", "false");
			map.put("Info", e.getMessage());
			// e.printStackTrace();
		} catch (IOException e) {
			map.put("flag", "false");
			map.put("Info", e.getMessage());
			// e.printStackTrace();
		}
		return JSONObject.fromObject(map).toString();

	}

}
